define(["jquery","bootstrap","template","bbcare","oms_tpl/projects.tpl"],function($,$bootstrap,template,$bb,$tpl){
	var projects = {};

	/**
	 * 处理时间
	 */
	var getTime = function(loginTime){
		var date1 = new Date(loginTime);
		var date2 = new Date();
		var s1 = date1.getTime();
		var s2 = date2.getTime();
		var second = parseInt( (s2 - s1) / 1000 );
		var str = "";
		if( parseInt(second / 60) <= 0 ){
			str = second + "秒前";
		}else if( parseInt(second / 60 / 60) <= 0 ){
			str = parseInt(second / 60) + "分钟前";
		}else if( parseInt(second / 60 / 60 / 24) <= 0 ){
			str = parseInt(second / 60 / 60) + "小时前";
		}else if( parseInt(second / 60 / 60 / 24 / 7) <= 0 ){
			str = parseInt(second / 60 / 60 / 24) + "天前";
		}else if( parseInt(second / 60 / 60 / 24 / 30) <= 0 ){
			str = parseInt(second / 60 / 60 / 24 / 7) + "周前";
		}else if( parseInt(second / 60 / 60 / 24 / 365) <= 0 ){
			str = parseInt(second / 60 / 60 / 24 / 30) + "月前";
		}else{
			str = parseInt(second / 60 / 60 / 24 / 365) + "年前";
		}
		return str;
	}

	/**
	 * 处理租户信息
	 */
	var handleObj = function(obj,app){
		obj.tenantName = app.tenantName;
		obj.tenantIcon = app.tenantIcon;
		obj.appAlias = app.appAlias;
		obj.tenantId = app.tenantId;
		obj.appId = app.id;
		obj.loginTime = getTime(obj.loginTime);
		return obj;
	}

	/**
	 * 获取列表数据，租户和项目的展示
	 */
	var getApps = function(apps){
		var arr = [];
    	for (var i = 0; i < apps.length; i++) {
    		if( apps[i].auProjectRoleRel && apps[i].auProjectRoleRel.length > 0 ){
    			for (var j = 0; j < apps[i].auProjectRoleRel.length; j++) {
    				var obj = {};
    				obj.projectName = apps[i].auProjectRoleRel[j].projectName;
    				obj.projectId = apps[i].auProjectRoleRel[j].projectId;
    				obj.projectIcon = apps[i].auProjectRoleRel[j].logo;
    				obj.loginTime = apps[i].auProjectRoleRel[j].loginTime;
    				obj = handleObj(obj,apps[i]);
    				arr.push(obj);
    			}
    		}else{
    			var obj = {};
    			obj.loginTime = apps[i].loginTime;
    			obj = handleObj(obj,apps[i]);
    			arr.push(obj);
    		}
    	}
    	return arr;
	}

	/**
	 * 退出系统处理
	 */
	var onLogout = function(){
		window.location.href = "login.html";
	}

	/**
	 * 处理apps数据
	 */
	var handleApps = function(projectId,appId){
		var account = JSON.parse( $bb.getSessionStorage("account") );
		for (var i = 0; i < account.apps.length; i++) {
			if( account.apps[i].id == appId ){
				account.apps[i].status = 1;
				if( account.apps[i].auProjectRoleRel && account.apps[i].auProjectRoleRel.length > 0){
					for (var j = 0; j < account.apps[i].auProjectRoleRel.length; j++) {
	 					account.apps[i].auProjectRoleRel[j].status = "1";
	 					if( account.apps[i].auProjectRoleRel[j].projectId == projectId ){
							account.apps[i].auProjectRoleRel[j].selectStatus = 1;
						}else{
							account.apps[i].auProjectRoleRel[j].selectStatus = 2;
						}
					}
				}
			}else{
				account.apps[i].status = 2;
				if( account.apps[i].auProjectRoleRel && account.apps[i].auProjectRoleRel.length > 0){
					for (var j = 0; j < account.apps[i].auProjectRoleRel.length; j++) {
	 					account.apps[i].auProjectRoleRel[j].status = "2";
	 					account.apps[i].auProjectRoleRel[j].selectStatus = 2;
					}
				}
			}
		}
		return account;
	}

	/**
	 * 选择租户或项目
	 */
	var onProjectSelect = function(){
		var projectId = $(this).attr("data-projectId");
		var appId = $(this).attr("data-appId");
		var account = handleApps(projectId,appId);
		$bb.setSessionStorage("account",JSON.stringify(account));
		window.location.href = "home.html";
	}

	projects.init = function(){
		var account = JSON.parse( $bb.getSessionStorage("account") );
		//初始化用户名称
    	$("#oms_account_name").html(account.alias);
    	//初始化登出事件
		$("#logout").click(onLogout);
		//初始化租户或项目列表
    	var list = getApps(account.apps);
    	$tpl.init("projects");
    	$("#projects").html( template("tpl_project_list",{"list":list}) );
    	//选择某一租户或者某一项目
    	$("#projects li").unbind("click");
    	$("#projects li").click(onProjectSelect);
	}
    
	return projects;
});